<?php
	require_once("../conexion/conexion.php");
    require_once("../Mantenedores/cliente.php");
	require_once("../Funciones/funciones_publicas.php");
	
	function BuscaDatos()
	{
		$con = new Conexion();
		$con2 = $con->conexionBD("fpproduccion");
		$sql = "select distinct(f.AECod),AENombre,date_format(FacFecha,'%d-%m-%Y') as fecha from facturatmp f inner join areaextraccion ae on f.AECod=ae.AECod";
		$resultado = mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
		$datos = mysql_fetch_array($resultado);
		$con->cerrarBD();
		return $datos;
	}
	
	function BuscaDatosCliente($rut)
	{
		$con = new Conexion();
		$con2 = $con->conexionBD("fpproduccion");
		$sql = "select 	CliRSocial,
						CliDireccion,
						CliRegion,
						CliFono,
						CliGiro,
						CliComuna,
						FacTotal,
						ConVentaDescrip,
						FacPorcentaje
		
						from clientes c
						inner join facturatmp f on 
						f.CliRut=c.CliRut 
						inner join condicionventa cv on 
						cv.ConVentaCod=f.ConVentaCod 
						where f.CliRut='".$rut."'";
						
		$res= mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
		$datos = mysql_fetch_array($res);
		$con->cerrarBD();
		return $datos;
	}
	
	$rut = $_POST['rut'];
    $cli = new Cliente();
	$fun = new funciones_publicas();
	
	$datos = BuscaDatos();
	$datosCliente = BuscaDatosCliente($rut);
	
	$fecha = explode("-",$datos[2]);
	$dia = $fecha[0];
	$mes = $fecha[1];

	if($mes==1)
	{	
		$mesDetalle = 12;
		$agnoDetalle = $fecha[2]-1;
	}
	else
	{	
		$mesDetalle = ($mes-1);
		$agnoDetalle = $fecha[2];
	}

	$agno = substr($fecha[2], -2); 
    //$fecha = "$fecha[2]-$fecha[1]-$fecha[0]";
	$palMes = $fun->SeleccionaMes($mes);
	$palMesDetalle = $fun->SeleccionaMes($mesDetalle);
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento sin t&iacute;tulo</title>
<script src="../include_js/jquery.js" type="text/javascript"></script>
<style>
.area
{
	font-family:Arial;
	font-style:normal;
	font-size:9pt;
	color:#000000;
	border-style:solid; 
	border-top-width:0px; 
	border-right-width:0px; 
	border-bottom-width:0px;
	border-left-width:0px;
	overflow:hidden;
}
.caja2
{
	font-family:Arial;
	font-style:normal;
	font-size:12pt;
	color:#000000;
	text-decoration: none;
	background-color: #FFFFFF;
	border: 0px;
	border-left: 0px solid #6C6C6C;
	border-right: 0px solid #6C6C6C;
	text-align:right;
}
.resp
{
	font-family:Arial;
	font-style:normal;
	font-size:10pt;
	color:#000000;
}
label.texto
{
    font-family:Arial;
	font-style:normal;
	font-size:10pt;
	color:#000000;
}
.caja
{
	font-family:Arial;
	font-style:normal;
	font-size:9pt;
	color: #000000;
	text-decoration: none;
	background-color: #FFFFFF;
	border: 0px;
	border-left: 0px solid #6C6C6C;
	border-right: 0px solid #6C6C6C;
}
.texto
{
	font-family:Arial;
	font-style:normal;
	font-size:10pt;
	color:#000000;
	text-align:center;
}
.digito
{
	font-family:Arial;
	font-style:normal;
	font-size:10pt;
	color:#000000;
}
.datos
{
	font-family:Arial;
	font-style:normal;
	font-size:8pt;
	color:#000000;
	text-align:center;
}
</style>
</head>

<body>
<form name="form_item_fac" id="form_item_fac" method="post">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td colspan="2" style="height:180px;">&nbsp;</td>
  </tr>
  <tr>
    <td width="79%">&nbsp;</td>
    <td width="21%">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2"><div style="padding-left:60px"><label class="texto"><?php echo $dia;?></label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label class="texto"><?php echo $palMes;?></label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label class="texto"><?php echo $agno;?></label></div></td>
  </tr>
  <tr>
	  <td><div style="padding-left:65px; padding-top:12px;">
	    <label class="datos"><?php echo $datosCliente[0];?></label>
	  </div></td>
    <td><div style="padding-top:12px;"><label class="digito"><?php echo $rut;?></label></div></td>
  </tr>
  <tr>
	<td><div style="padding-left:65px;padding-top:12px;"><label class="datos"><?php echo $datosCliente[1];?></label></div></td>
    <td><div style="padding-top:12px;"><label class="datos"><?php echo $datosCliente[5];?></label></div></td>
  </tr>
  <tr>
	<td><div style="padding-left:40px;padding-top:12px;"><label class="datos"><?php echo $datosCliente[4];?></label></div></td>
    <td><div style="padding-top:12px;"><label class="digito"><?php echo $datosCliente[3];?></label></div></td>
  </tr>
  <tr>
	<td>&nbsp;</td>
    <td><div style="padding-top:12px;"><label class="datos"><?php echo $datosCliente[7];?></label></div></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>


<table width="100%" cellpadding="4" cellspacing="0" border="0" id="tablaDetalle">
<!--<tr>
	<td width="12%">&nbsp;</td>
	<td width="56%">&nbsp;</td>
	<td width="20%">&nbsp;</td>
	<td width="12%">&nbsp;</td>
</tr>-->
<tr><td colspan="4" style="height:30px;">&nbsp;</td></tr>
<?php
			$sumatoria=0;
			//PSMB
			$con = new Conexion();
			$con2 = $con->conexionBD("fpproduccion");
			$sql = "select FacPsmb,FacPorcentaje from facturatmp where CliRut='".$rut."'";
			$res= mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
			$con->cerrarBD();
			while($row = mysql_fetch_array($res))
			{
				$cadena = "Muestreos y analisis muestras Programa de Moluscos Bivalvos PSMB, Area ".$datos[1]." mes ".$palMesDetalle." del ".$agnoDetalle.", correspondiente a un % ".$row['FacPorcentaje']."";
				echo "<tr>";
				echo "<td width='12%' style='text-align:left;padding-left:20px;vertical-align:top;'><label class='digito'>1</label></td>";
				echo "<td width='56%' class='resp'><div style='width:350px;'>".$cadena."</div></td>";
				echo "<td width='20%' style='text-align:right;padding-right:60px;vertical-align:top;'><label class='digito'>".number_format ($row['FacPsmb'],0,',','.')."</label></td>";
				echo "<td width='12%' style='text-align:right;padding-right:30px;vertical-align:top;'><label class='digito'>".number_format ($row['FacPsmb'],0,',','.')."</label></td>";
				echo "</tr>";
				$sumatoria = $sumatoria + $row['FacPsmb'];
			}
			
			
			//valores del muestreo
			$con = new Conexion();
			$con2 = $con->conexionBD("fpproduccion");
			$sql = "select FacCantMuestreo,FacPorcentaje,FacUFMuestreo,FacUF,FacTotalMuestreo from facturatmp where CliRut='".$rut."'";
			$res= mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
			while($row = mysql_fetch_array($res))
			{
				//$unitario = ($row['FacUFMuestreo']*$row['FacUF']);
				//$unitario = round($unitario);
				$total = $row['FacTotalMuestreo'];
				$total = round($total);
				echo "<tr>";
				echo "<td width='12%' style='text-align:left;padding-left:20px;vertical-align:top;'><label class='digito'>".$row['FacCantMuestreo']."</label></td>";
				echo "<td width='56%' class='resp'><div style='width:350px;'>Valor del Muestreo Fishing Partners, correspondiente a un %".$row['FacPorcentaje']."</div></td>";
				echo "<td width='20%' style='text-align:right;padding-right:60px;vertical-align:top;'><label class='digito'>".number_format ($total,0,',','.')."</label></td>";
				echo "<td width='12%' style='text-align:right;padding-right:30px;vertical-align:top;'><label class='digito'>".number_format ($total,0,',','.')."</label></td>";
				echo "</tr>";
				$sumatoria = $sumatoria + $total;
			}
			$con->cerrarBD();
			
			//se cargan los otros valores
			$con = new Conexion();
			$con2 = $con->conexionBD("fpproduccion");
			$sql = "select ItemFacCant,ItemFacGlosa,ItemFacUnitario,ItemFacTotal from itemfacturatmp";
			$res= mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
			while($row = mysql_fetch_array($res))
			{
					$total = ($datosCliente[8]*$row['ItemFacTotal'])/100;
					$total = round($total);
					echo "<tr>";
					echo "<td width='12%' style='text-align:left;padding-left:20px;vertical-align:top;'><label class='digito'>".$row[0]."</label></td>";
					echo "<td width='56%' class='resp'><div style='width:350px;'>".$row[1]."</div></td>";
					echo "<td width='20%' style='text-align:right;padding-right:60px;vertical-align:top;'><label class='digito'>".number_format ($total,0,',','.')."</label></td>";
					echo "<td width='12%' style='text-align:right;padding-right:30px;vertical-align:top;'><label class='digito'>".number_format ($total,0,',','.')."</label></td>";
					echo "</tr>";
					$sumatoria = $sumatoria + $total;
			}
			$con->cerrarBD();
			
			$subTotal = $sumatoria;
			$iva = round($subTotal*0.19);
			$total = ($subTotal+$iva);
			$pal=$fun->numletras($total);
			?>
</table>
<div style="position:absolute; top:710px; left:80px; width:350px"><label style="font-family:Arial; font-size:10pt; font-style:italic;">DEPOSITAR EN CUENTA CTE N&deg; 60310391 BANCO BCI A FISHING PARTNERS LTDA</label></div>
<div style="position:absolute; top:760px; left:30px"><textarea name="txtPalabras" cols="70" rows="3" class="area"><?php echo $pal;?></textarea></div>
<div style="position:absolute; top:800px; left:500px;"><input name="txtSubTotal" type="text" class="caja2" readonly="true"  size="12" value="<?php echo number_format ($subTotal,0,',','.')?>"/></div>
<div style="position:absolute; top:830px; left:500px;"><input name="txtIVA" type="text" class="caja2" readonly="true" size="12" value="<?php echo number_format ($iva,0,',','.')?>"/></div>
<div style="position:absolute; top:860px; left:500px;"><input name="txtTotal" type="text"  class="caja2" readonly="true" size="12" value="<?php echo number_format ($total,0,',','.')?>" /></div>
<div style="position:absolute; top:820px; left:450px;"><input name="txtImp" type="text"  class="caja2" readonly="true" size="1" value="19" /></div>

</form>
</body>
</html>